﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using PessModel;
using PessMvc.Models;
namespace PessMvc.Lib
{
    public class ExpertJudgeBL:IExpertJudgeBL
    {
        private PESSEntities db;

        public ExpertJudgeBL()
        {
            db = new PESSEntities();
        }

        public bool Add(ExpertJudge expert, List<ExpertGroup> expertGroups,out string errorMsg, string password = "111111")
        {
            //if (expertGroups == null || expertGroups.Count == 0)
            //{
            //    errorMsg = "没有选择评委组！请选择所属评委组。";
            //    return false;
            //}
            MembershipCreateStatus createStatus;
            expert.Email=expert.Email == null ? expert.SerialNumber + "@b.com" : expert.Email;
            MembershipUser user = Membership.CreateUser(expert.SerialNumber, password, expert.Email, "question", "answer", true, null, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(user.UserName, "ExpertJudge");
                expert.UserID = new Guid(user.ProviderUserKey.ToString());

                //db.ExpertGroups.Find().ExpertJudges.Add

                expert.ExpertGroups = null;
                db.ExpertJudges.Add(expert);
                db.SaveChanges();

                if (!(expertGroups == null || expertGroups.Count == 0))
                {
                    foreach (var group in expertGroups)
                    {
                        var g = db.ExpertGroups.Find(group.ID);
                        g.ExpertJudges.Add(expert);
                    }
                    db.SaveChanges();
                }

                user.Email = expert.Email;
                Membership.UpdateUser(user);   
                errorMsg = null;

                //expert = db.ExpertJudges.Find(expert.SerialNumber);
                

                return true;// RedirectToAction("Details", "ExpertGroup", new { id = expert.ExpertGroupID });
            }
            else
            {
                errorMsg = createStatus.ToString();
                return false;
                //ModelState.AddModelError("", createStatus.ToString());
            }
        }

        public bool IfExpertExists(int? expertGroupID, string realName)
        {
            bool ifExists = false;
            int count = db.ExpertJudges.Where(e => e.ExpertGroupID == expertGroupID && e.RealName.Trim() == realName.Trim()).Count();
            if (count >= 1)//Expert with the same realname already exist in the same expert group.
            {
                ifExists = true;
            }
            return ifExists;
        }
        public bool IfExpertExists2(ExpertJudgeModel model)
        {
            bool ifExists = false;
            int count = (from e in db.ExpertJudges
                         where e.SerialNumber == model.SerialNumber ||
                         e.RealName == model.RealName && e.Gender == model.Gender
                         select e).Count();
            if (count >= 1)
            {
                ifExists = true;
            }
            return ifExists;
        }

        public bool IfExpertGroupMatchesActivity(int activityID, int? expertGroupID)
        {
            bool flagValue = false;
            var expertGroup = db.ExpertGroups.Find(expertGroupID);
            if (expertGroup != null && expertGroup.ActivityID == activityID)
            {
                flagValue = true;
            }
            return flagValue;
        }

        public ExpertJudge Find(string serialNumber)
        {
            var judge=db.ExpertJudges.Find(serialNumber);
            if (judge == null)
            {
                return judge;
            }

            var user = Membership.GetUser(serialNumber.Trim());
            judge.Email = user.Email;

            return judge;
        }

        public ExpertJudge Convert(ExpertJudgeModel model)
        {
            ExpertJudge expertJudge = new ExpertJudge()
            {
                BirthDate = model.Birthdate,
                ContactAddress = model.ContactAddress,
                Email = model.Email,
                Gender = model.Gender,
                Phone = model.Phone,
                RealName = model.RealName,
                SerialNumber = model.SerialNumber,
                ZipCode = model.ZipCode
            };
            return expertJudge;
        }
        public ExpertJudgeModel Convert(ExpertJudge expert)
        {
            ExpertJudgeModel expertJudge = new ExpertJudgeModel()
            {
                Birthdate = expert.BirthDate,
                ContactAddress = expert.ContactAddress,
                Email = expert.Email,
                Gender = expert.Gender,
                Phone = expert.Phone,
                RealName = expert.RealName,
                SerialNumber = expert.SerialNumber,
                ZipCode = expert.ZipCode
            };
            return expertJudge;
        }
        public bool IfAllContractsSigned(string serialNumber)
        {
            var judge = db.ExpertJudges.Find(serialNumber);
            var count = (from c in judge.ContractAssignments
                         where c.Signed != true
                         select c).Count();
            if (count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}